<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('data');
	}
	
	public function index() {
		$this->_get_userinfo();
		$id = $this->input->get('id');
		$query = '?id='.$id;
		$student = $this->_validate_id($id);
		$this->load->library('scholarship');
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'student', $query));
		$params = array(
			'role' => $this->userinfo['role'],
			'student' => $student,
			'query' => $query,
			'scholarships' => $this->scholarship->get_scholarship_history($student->username),
			'registrations' => $this->scholarship->get_register_history($student->username)
		);
		$this->load->view('common/student_profile', $params);
		$this->_load_common_footer();
	}
	
	public function photo() {
		$this->_get_userinfo();
		$id = $this->input->get('id');
		$query = '?id='.$id;
		$student = $this->_validate_id($id);
		
		$this->load->helper('download');
		$filename = do_hash($student->username.'.jpg', 'sha1');
		$pathfile = 'files/userphoto/'.$filename;
		if (file_exists($pathfile)) {
			$name = $student->username.'.jpg';
		} else {
			$pathfile = 'files/userphoto/'.do_hash('default.jpg', 'sha1');
			$name = 'default.jpg';
		}
		$data = file_get_contents($pathfile);
		force_download($name, $data);
	}
	
	public function achievement($offset = '0') {
		$this->_get_userinfo();
		$id = $this->input->get('id');
		$query = '?id='.$id;
		$student = $this->_validate_id($id);
		$this->load->helper('table');
		$limit = $this->data->count_achievement($student->username);
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'student', $query));
		$params = array(
			'achievements' => $this->data->get_achievement($student->username, '0', $limit),
			'student' => $student,
			'pager' => get_pager(3)
		);
		$this->load->view('common/student_achievement', $params);
		$this->_load_common_footer();
	}
	
	public function organization() {
		$this->_get_userinfo();
		$id = $this->input->get('id');
		$query = '?id='.$id;
		$student = $this->_validate_id($id);
		$this->load->helper('table');
		$limit = $this->data->count_organization($student->username);
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'student', $query));
		$params = array(
			'student' => $student,
			'organizations' => $this->data->get_organization($student->username, '0', $limit),
			'pager' => get_pager(3)
		);
		$this->load->view('common/student_organization', $params);
		$this->_load_common_footer();
	}
	
	public function document() {
		$this->_get_userinfo();
		$id = $this->input->get('id');
		$query = '?id='.$id;
		$student = $this->_validate_id($id);
		$this->load->library('file');
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'student', $query));
		$params = array(
			'query' => $query,
			'student' => $student,
			'downloadstatus' => $this->session->flashdata('downloadstatus'),
			'notfoundfile' => $this->session->flashdata('notfoundfile'),
			'role' => $this->userinfo['role'],
			'files' => $this->file->get_files($student->username)
		);
		$this->load->view('common/student_document', $params);
		$this->_load_common_footer();
	}
	
	public function download($fileid = NULL) {
		$this->_get_userinfo();
		$id = $this->input->get('id');
		$query = '?id='.$id;
		$student = $this->_validate_id($id);
		$this->load->library('file');
		
		if ($fileid == NULL) {
			redirect('mahalum/student/profile/document'.$query, 'location');
			exit;
		}
		$file = $this->file->get_category_by_username_and_id($student->username, $fileid);
		if ($file == NULL) {
			redirect('mahalum/student/profile/document'.$query, 'location');
			exit;
		}
		$this->load->helper('download');
		$filename = do_hash($student->username.' - '.$file->name.'.pdf', 'sha1');
		$pathfile = 'files/studentdocument/'.$filename;
		if (file_exists($pathfile)) {
			$name = $student->username.' - '.$file->name.'.pdf';
			$data = file_get_contents($pathfile);
			force_download($name, $data);
			$this->session->set_flashdata('downloadstatus', 'success');
			redirect('mahalum/student/profile/document'.$query, 'location');
		} else {
			$this->session->set_flashdata('downloadstatus', 'failed');
			$this->session->set_flashdata('notfoundfile', $file->name);
			redirect('mahalum/student/profile/document'.$query, 'location');
		}
	}
	
	public function history() {
		$this->_get_userinfo();
		$id = $this->input->get('id');
		$query = '?id='.$id;
		$student = $this->_validate_id($id);
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'student', $query));
		$params = array(
			'student' => $student,
			'histories' => $this->data->get_histories($student->username)
		);
		$this->load->view('common/student_history', $params);
		$this->_load_common_footer();
	}
	
	public function story() {
		$this->_get_userinfo();
		$id = $this->input->get('id');
		$query = '?id='.$id;
		$student = $this->_validate_id($id);
		$story = $this->data->get_story($student->username);
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'student', $query));
		$params = array(
			'story' => $story,
			'student' => $student
		);
		$this->load->view('common/student_story', $params);
		$this->_load_common_footer();
	}
	
	private function _validate_id($id) {
		if ($id == FALSE) {
			redirect('mahalum/student/search/index', 'refresh');
			exit;
		}
		$student = $this->data->get_student_by_id($id);
		if ($student == NULL) {
			redirect('mahalum/student/search/index', 'refresh');
			exit;
		} else {
			return $student;
		}
	}
}